#include "../include/proto/codec.h"
#include "../include/proto/dispatcher.h"
#include "../include/muduo/base/Logging.h"
#include "../include/muduo/base/Mutex.h"
#include "../include/muduo/net/EventLoop.h"
#include "../include/muduo/net/TcpServer.h"

#include <iostream>
#include "../include/muduo/net/TcpClient.h"
#include "../include/muduo/net/EventLoopThread.h"
#include "../include/muduo/net/TcpConnection.h"
#include "../include/muduo/base/CountDownLatch.h"
#include <functional>
#include <string>
#include <memory>
#include "request.pb.h"

class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<Message_Queue::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<Message_Queue::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<Message_Queue::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<Message_Queue::AddResponse> AddResponsePtr;

    Server(int port)
        :_server(&_baseloop,muduo::net::InetAddress("0.0.0.0",port),"Translate-Server"
        ,muduo::net::TcpServer::kReusePort)
        ,_dispatcher(std::bind(&Server::onUnknownMessage,this,std::placeholders::_1
            ,std::placeholders::_2
            ,std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher
            ,std::placeholders::_1
            ,std::placeholders::_2
            ,std::placeholders:: _3)
        )
    {
        // 注册业务请求处理函数。
        _dispatcher.registerMessageCallback<Message_Queue::TranslateRequest>(
        std::bind(&Server::onTranslate, this, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));
        _dispatcher.registerMessageCallback<Message_Queue::AddRequest>(
        std::bind(&Server::onAdd, this, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));
        _server.setConnectionCallback(
        std::bind(&Server::onConnection, this, std::placeholders::_1));
        _server.setMessageCallback(
        std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));;
    }
    void Start()
    {
        _server.start();
        _baseloop.loop();
    }
private:
    std::string Translate(const std::string& s)
    {
        // 这里只是随便测一下的。
        static std::unordered_map<std::string,std::string> translate = 
        {
            { "Hello" , "你好" } ,
            { "Healthy" , "健康" }
        };

        std::unordered_map<std::string,std::string>::iterator pos = translate.find(s);
        if(pos == translate.end())
        {
            return "None";
        }

        return pos->second;
    }
    void onTranslate(const muduo::net::TcpConnectionPtr conn
        ,const TranslateRequestPtr& message
        ,muduo::Timestamp)
    {
        // 1.提取
        std::string req_msg = message->msg();
        // 2.翻译
        Translate(req_msg);
        // 3.组织响应
        Message_Queue::TranslateResponse resp;
        resp.set_msg(req_msg);
        // 4.发送响应
        _codec.send(conn,resp);
    }
    void onAdd(const muduo::net::TcpConnectionPtr& conn
        ,const AddRequestPtr& message
        ,muduo::Timestamp)
    {
        // 1.提取
        int num1 = message->num1();
        int num2 = message->num2();
        // 2.获取结果
        int result = num1 + num2;
        // 组织响应
        Message_Queue::AddResponse resp;
        resp.set_result(result);
        // 发送响应。
        _codec.send(conn,resp);
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn
        ,const MessagePtr& message,muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected() == true)
        {
            LOG_INFO << "新链接已建立" ;
        }
        else
        {
            LOG_INFO << "链接即将关闭" ;
        }
    }
private:
    muduo::net::EventLoop _baseloop; // 执行
    muduo::net::TcpServer _server;  // 服务端
    ProtobufDispatcher _dispatcher; // 分发器
    ProtobufCodec _codec;  // 协议处理器
};

int main()
{
    Server server(8085);
    server.Start();

    return 0;
}